package com.hnevc.trafficexpanedlistviewdemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;

import com.hnevc.trafficexpanedlistviewdemo.httppost.HttpThread;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class TwoListViewActivity extends AppCompatActivity {


    private static final String TAG = "TwoListViewActivity";

    ListView mListView1, mListView2;

    String url = "http://192.168.1.142:8080/transportservice/type/jason/action/GetBusStationInfo.do";
    String jsonString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two_list_view);
        mListView1 = (ListView) findViewById(R.id.id_lv_lxds);
        mListView2 = (ListView) findViewById(R.id.id_lv_zyy);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int i=0;
            @Override
            public void run() {
                i=(i++)%2+1;
                queryBusStationInfo(i);
            }
        },0, 3000);


    }

    private void queryBusStationInfo(int busStationId) {
        JSONObject object = new JSONObject();
        try {
            object.put("BusStationId", busStationId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonString = object.toString();

        //创建线程，启动线程
        HttpThread httpThread = new HttpThread(this, mHandler);
        httpThread.setJsonstring(jsonString);
        httpThread.setUrl(url);
        httpThread.start();
    }


    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            // if (msg.what == 1) {
            if (msg.what == 1 || msg.what == 901) {

                String strWebContent = null;
                strWebContent = (String) msg.obj;
                Log.e(TAG, "服务器返回的数据" + strWebContent + msg.what);
                String[] msgs = strWebContent.split("=");
                strWebContent = strWebContent.substring(0, msgs[0].length() - 1);
                String jsonString = msgs[1];
                JSONObject requestJsonObject = null;
                try {
                    requestJsonObject = new JSONObject(jsonString);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //Log.e(TAG, "handleMessage: " + strWebContent);
                //解析数据，构建适配器
                JSONArray array;
                try {
                    array = new JSONArray(strWebContent);
                    List<BusInfo> busInfoList = new ArrayList<>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object = array.getJSONObject(i);
                        BusInfo busInfo = new BusInfo();
                        busInfo.setBusId(object.getInt("BusId"));
                        busInfo.setDistance(object.getInt("Distance"));
                        busInfo.setArriveTime(object.getInt("Distance") / (20000 / 60));
                        busInfo.setPersonNum(101);
                        busInfoList.add(busInfo);
                    }
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("name", requestJsonObject.getInt("BusStationId") == 1 ? "联想大厦" : "中医院");
                    map.put("busInfoList", busInfoList);

                    MyBaseAdapter adapter = new MyBaseAdapter(TwoListViewActivity.this,
                            busInfoList);

                    if (requestJsonObject.getInt("BusStationId") == 1) {
                        mListView1.setAdapter(adapter);
                    } else {
                        mListView2.setAdapter(adapter);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        }
    };

}
